import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/ute/uteped_const.dart';

import 'dnd_const.dart';
import 'dnd_state.dart';

class DndLogic extends BaseController {
  final DndState state = DndState();

  void queryDndMode() {
    dRemoteReqAsync(DndConst.methodQueryDeviceDoNotDisturb, {})
        .then((data) => _onQueryDndMode(data));
  }

  void _onQueryDndMode(data) {
    if (data[UTEPedConst.keySuccess]) {
      state.dndMode.value = data[DndConst.keyAllDayEnable];
      state.smartDndMode.value = data[DndConst.keyTimingEnable];

      /// sdk 有bug，暂时屏蔽
      // state.startTimeHour = data[DndConst.keyFromTimeHour];
      // state.startTimeMin = data[DndConst.keyFromTimeMinute];
      // state.startTime.value = _getTimeAs12Hour(state.startTimeHour, state.startTimeMin);
      //
      // state.endTimeHour = data[DndConst.keyToTimeHour];
      // state.endTimeMin = data[DndConst.keyToTimeMinute];
      // state.endTime.value = _getTimeAs12Hour(state.endTimeHour, state.endTimeMin);
    }
  }

  void setDndMode(value) {
    showLoading();
    state.dndMode.value = value;
    bRemoteReqAsync(DndConst.methodSetDoNotDisturb, {DndConst.keyAllDayEnable: value})
        .then((success) => {
          cancelLoading(),
          if (success) {
            save(DndConst.keyAllDayEnable, value),
            if (value && state.smartDndMode.value) {
              setSmartDndMode(false)
            }
          }
          else {
            state.dndMode.value = !value
          }
    });
  }
  void setSmartDndMode(value) {
    showLoading();
    state.smartDndMode.value = value;
    bRemoteReqAsync(DndConst.methodSetDoNotDisturb, {DndConst.keyTimingEnable: value})
        .then((success) => {
      cancelLoading(),
      if (success) {
        save(DndConst.keyTimingEnable, value)
      }
      else {
        state.smartDndMode.value = !value
      }
    });
  }

  void setStartTime(hour, min) {
    showLoading();
    state.startTimeHour = hour;
    state.startTimeMin = min;
    state.startTime.value = _getTimeAs12Hour(hour, min);

    bRemoteReqAsync(DndConst.methodSetDoNotDisturb, {DndConst.keyFromTimeHour: hour, DndConst.keyFromTimeMinute:min})
        .then((success) => {
      cancelLoading(),
      if (success) {
        save(DndConst.keyFromTimeHourDnd, hour), save(DndConst.keyFromTimeMinuteDnd, min)
      }
    });
  }

  void setEndTime(hour, min) {
    showLoading();
    state.endTimeHour = hour;
    state.endTimeMin = min;
    state.endTime.value = _getTimeAs12Hour(hour, min);

    bRemoteReqAsync(DndConst.methodSetDoNotDisturb, {DndConst.keyToTimeHour: hour, DndConst.keyToTimeMinute:min})
        .then((success) => {
      cancelLoading(),
      if (success) {
        save(DndConst.keyToTimeHourDnd, hour), save(DndConst.keyToTimeMinuteDnd, min)
      }
    });
  }

  void setExtraStartTimeEnable(value) {
    state.extraStartTimeEnable.value = value;
  }

  void setExtraEndTimeEnable(value) {
    state.extraEndTimeEnable.value = value;
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    state.dndMode.value = read(DndConst.keyAllDayEnable, defaultValue: false);
    state.smartDndMode.value = read(DndConst.keyTimingEnable, defaultValue: false);

    state.startTimeHour = read(DndConst.keyFromTimeHourDnd, defaultValue: 8);
    state.startTimeMin = read(DndConst.keyFromTimeMinuteDnd, defaultValue: 0);
    state.startTime.value = _getTimeAs12Hour(state.startTimeHour, state.startTimeMin);

    state.endTimeHour = read(DndConst.keyToTimeHourDnd, defaultValue: 9);
    state.endTimeMin = read(DndConst.keyToTimeMinuteDnd, defaultValue: 0);
    state.endTime.value = _getTimeAs12Hour(state.endTimeHour, state.endTimeMin);

    queryDndMode();
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }

  String _getTimeAs12Hour(hour, min) {
    var tempHour = hour;
    if (hour > 12) {
      tempHour -= 12;
    }

    var tempMin = min.toString();
    if (min == 0) {
      tempMin = "00";
    }

    return "$tempHour:$tempMin";
  }
}
